<?php

namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Validator\EmailAddress;
use Zend\Validator\Regex;
use Zend\Mail\Message;
use Zend\Mail\Transport\Sendmail;
use Zend\Session\Container;

class ContactController extends AbstractActionController
{

    public function indexAction()
    {
        $test_session = new Container('test');
        $test = $test_session->username;
        if (isset($test) and $test == false){
            //AFFICHAGE SESSIONS
            $contact = new Container('contact');
            $username = $contact->nom;
            $tel = $contact->tel;
            $mail = $contact->mail;
            $contain = $contact->content;
        }else{
            $username = '';
            $tel = '';
            $mail = '';
            $contain = '';
        }
        
        
        //CREATION DU FORMULAIRE
        $urlAction = $this ->url()->fromRoute("envoieMail");
        $form = new \Zend\Form\Form('formContact');
        
        $form -> setAttributes(array('action' => $urlAction, 'method'=> 'post',));
        
        //input nom
        $form -> add(array(
            'name' => 'nom',
            'type' => 'text',
            'option' => array('label' => 'nom',
                              'label_attributes' => array('class' => 'form-control')
                              ),
            'attributes' => array('id' => 'nom',
                                  'class' => 'form-control',
                                  'required' => 'required',
                                  'placeholder' =>'Nom et Prenom ',
                                  'value' => $username)
        ));
        
        //input mail
        $form -> add(array(
            'name' => 'mail',
            'type' => 'text',            
            'option' => array('label' => 'mail',
                              'label_attributes' => array('class' => 'formElementLabel')
                              ),
            'attributes' => array('id' => 'mail',
                                  'class' => 'form-control',
                                  'required' => 'required',
                                  'placeholder' =>'Mail ',
                                  'value' => $mail)
        ));
        
        //input tel
        $form -> add(array(
            'name' => 'tel',
            'type' => 'text',
            
            'option' => array('label' => 'tel',
                              'label_attributes' => array('class' => 'formElementLabel')
                              ),
            'attributes' => array('id' => 'tel',
                                  'class' => 'form-control',
                                  'required' => 'required',
                                  'placeholder' =>'Telephone ',
                                  'value' => $tel)
        ));
        
        //textarea
       $form -> add(array(
            'name' => 'content',
            'attributes' => array(
                        'id'=> 'content',
                        'type' => 'textarea',
                        'class' => 'form-control',
                        'placeholder' =>'Votre Commentaire ',
                        'rows' => '10',
                        'value' => $contain),
             ));
        //Bouton de validation du formulaire
        $form-> add(array(
            'name' => 'valider',
            'type' =>'submit',
            'attributes' => array('id' => 'envoyer',
                                  'class' => 'btn btn-primary','value' => 'Valider')
        ));
                
        //on envoie le formulaire pour l'affichage
        return array('form' => $form);
    
    }
    public function sendMailAction(){
        //INITIALISATION SESSION
        $nom = $_POST['nom'];
        $validatorMail = new EmailAddress();
        
        $test= new Container('test');
        $test->username = true;
        $contact= new Container('contact');
        $contact->nom = $_POST['nom'];
        $contact->tel = $_POST['tel'];
        $contact->mail = $_POST['mail'];
        $contact->content = $_POST['content'];
        
        if ($validatorMail->isValid($_POST['mail'])) {
            // email appears to be valid
            $mail = $_POST['mail'];
        } else {
            // email is invalid; print the reasons
            foreach ($validatorMail->getMessages() as $messageId => $message) {
                echo "Validation failure '$messageId': $message\n";
            }
            $mail = false;
            $test->username = false;
            $this->redirect()->toRoute('returnContact');
            
        }
        $validator = new Regex(array('pattern' => '#^0[1-68]([-. ]?[0-9]{2}){4}$#'));
        if ($validator->isValid($_POST['tel'])) {
            // email appears to be valid
            $tel = $_POST['tel'];
        } else {
            // email is invalid; print the reasons
            foreach ($validator->getMessages() as $messageId => $message) {
                echo "Validation failure '$messageId': $message\n";
            }
            $tel = false;
            $test->username = false;
            $this->redirect()->toRoute('returnContact');
        }
        
        if(($tel !=false) && ($mail != false)){
            $mail = new Message();
            $mail->setFrom($_POST['mail'], $_POST['nom']);
            $mail->addTo('galopin01@hotmail.fr', 'Lea Laplise');
            $mail->setSubject('Demande de Contact');
            $mail->setBody($_POST['content']);
            
            session_destroy();
            $transport = new Sendmail();
            $transport->send($mail);
        }
    }
    
    


}

